#!/bin/bash

# 库存相关接口测试脚本

# 配置参数
SERVER_URL="http://localhost:8080"  # 服务器地址
TOKEN=""  # 登录后获取的Token

# 颜色配置
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 登录并获取Token
login() {
  echo -e "${YELLOW}正在登录获取Token...${NC}"
  LOGIN_RESULT=$(curl -s -X POST "${SERVER_URL}/login" \
    -H "Content-Type: application/json" \
    -d '{"username":"admin","password":"admin123"}')
  
  TOKEN=$(echo $LOGIN_RESULT | grep -o '"token":"[^"]*' | sed 's/"token":"//')
  
  if [ -z "$TOKEN" ]; then
    echo -e "${RED}登录失败，无法获取Token${NC}"
    exit 1
  else
    echo -e "${GREEN}登录成功，Token获取成功${NC}"
  fi
}

# 测试库存列表接口
test_stock_list() {
  echo -e "${YELLOW}测试库存列表接口...${NC}"
  RESULT=$(curl -s -X GET "${SERVER_URL}/system/stock/list" \
    -H "Authorization: Bearer ${TOKEN}")
  
  if [[ $RESULT == *"\"code\":200"* ]]; then
    echo -e "${GREEN}库存列表接口测试成功${NC}"
    echo "接口返回: $(echo $RESULT | head -c 200)..."
  else
    echo -e "${RED}库存列表接口测试失败${NC}"
    echo "错误信息: $RESULT"
  fi
}

# 测试库存详情接口
test_stock_info() {
  echo -e "${YELLOW}测试库存详情接口...${NC}"
  RESULT=$(curl -s -X GET "${SERVER_URL}/system/stock/1" \
    -H "Authorization: Bearer ${TOKEN}")
  
  if [[ $RESULT == *"\"code\":200"* ]]; then
    echo -e "${GREEN}库存详情接口测试成功${NC}"
    echo "接口返回: $(echo $RESULT | head -c 200)..."
  else
    echo -e "${RED}库存详情接口测试失败${NC}"
    echo "错误信息: $RESULT"
  fi
}

# 测试库存预警数量接口
test_stock_warning_count() {
  echo -e "${YELLOW}测试库存预警数量接口...${NC}"
  RESULT=$(curl -s -X GET "${SERVER_URL}/system/stock/warning/count" \
    -H "Authorization: Bearer ${TOKEN}")
  
  if [[ $RESULT == *"\"code\":200"* ]]; then
    echo -e "${GREEN}库存预警数量接口测试成功${NC}"
    echo "接口返回: $RESULT"
  else
    echo -e "${RED}库存预警数量接口测试失败${NC}"
    echo "错误信息: $RESULT"
  fi
}

# 测试库存预警列表接口
test_stock_warning_list() {
  echo -e "${YELLOW}测试库存预警列表接口...${NC}"
  RESULT=$(curl -s -X GET "${SERVER_URL}/system/stock/warning/list" \
    -H "Authorization: Bearer ${TOKEN}")
  
  if [[ $RESULT == *"\"code\":200"* ]]; then
    echo -e "${GREEN}库存预警列表接口测试成功${NC}"
    echo "接口返回: $(echo $RESULT | head -c 200)..."
  else
    echo -e "${RED}库存预警列表接口测试失败${NC}"
    echo "错误信息: $RESULT"
  fi
}

# 执行测试
main() {
  echo -e "${YELLOW}开始测试库存相关接口...${NC}"
  login
  test_stock_list
  test_stock_info
  test_stock_warning_count
  test_stock_warning_list
  echo -e "${YELLOW}测试完成!${NC}"
}

main 